package com.ytjj.qmyx.admin.controller.imController;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.model.ImSessionInfo;
import com.ytjj.qmyx.admin.model.dto.ImMessageHistoryResponseDto;
import com.ytjj.qmyx.admin.model.dto.ImMessageInfoDto;
import com.ytjj.qmyx.admin.model.response.im.ImMessageInfoResponse;
import com.ytjj.qmyx.admin.model.vo.ImMessageInfoVO;
import com.ytjj.qmyx.admin.service.imService.ImMessageInfoService;
import com.ytjj.qmyx.admin.service.imService.ImSessionInfoService;
import com.ytjj.qmyx.admin.service.imService.ImUserBindInfoService;
import com.ytjj.qmyx.admin.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会话信息controller
 */
@RestController
@RequestMapping("/imMessage")
@Slf4j
public class ImMessageInfoController {

    @Autowired
    private ImMessageInfoService imMessageInfoService;
    @Autowired
    private ImSessionInfoService imSessionInfoService;
    @Autowired
    private ImUserBindInfoService imUserBindInfoService;

    /**
     *  获取聊天历史信息
     *
     * 参数:sessionId ----会话id
     *      isAll-----是否查全部
     * @return
     */
    @PostMapping("/getChatHistory")
    public CommonResult getChatHistory(@RequestBody ImMessageInfoVO imMessageInfoVO){
        ImMessageHistoryResponseDto dto = new ImMessageHistoryResponseDto();
        List<ImMessageInfoDto> list =  imMessageInfoService.getChatHistory(imMessageInfoVO.getSessionId(),imMessageInfoVO.getIsAll(),imMessageInfoVO.getIsAccess());
        ImSessionInfo imSessionInfo = imSessionInfoService.selectById(imMessageInfoVO.getSessionId());
        String customerName = imUserBindInfoService.selectCustomerName(imSessionInfo.getCustomerId());
        dto.setMessages(list);
        dto.setReceiveName(StringUtils.isNotBlank(customerName)?customerName:imSessionInfo.getCustomerId());
        dto.setSecondGap(DateUtils.getDateGapBySecond(imSessionInfo.getCreateTime()));
        return CommonResult.success(dto);
    }

    /**
     * 客服撤回消息-只有2分钟以内的消息能撤回
     * 参数: id---------会话id
     *
     * @param imMessageInfoVO
     * @return
     */
    @PostMapping("/recall")
    public CommonResult messageRecall(@RequestBody ImMessageInfoVO imMessageInfoVO) {
        log.info("===消息撤回:" + imMessageInfoVO.getId());
        return imMessageInfoService.messageRecall(imMessageInfoVO);
    }

    /**
     * 获取历史消息
     * @param userId 客户唯一id
     * @return
     */
    @GetMapping("/history")
    public CommonResult getHistoryMessage(String userId) {
        List<ImMessageInfoResponse> responses = imMessageInfoService.queryUserHistoryMessage(userId);
        return CommonResult.success(responses);
    }

    /**
     * 获取未读信息
     * @param receiveUserId
     * @return
     */
    @GetMapping("/unread")
    public CommonResult getunreadMessage(@RequestParam String receiveUserId) {
        return CommonResult.success(imMessageInfoService.selectUnreadMessage(receiveUserId));
    }
}
